/**
 * 
 */
package com.gs.logviewer.gui.comps;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.Serializable;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;

import com.gs.logviewer.enums.ExportTypeEnum;
import com.gs.logviewer.model.Log;
import com.gs.utils.collection.PaginatedCollection;
import com.gs.utils.enums.DisplayTypeEnum;
import com.gs.utils.swing.display.DisplayUtils;
import com.gs.utils.swing.file.ExtensionFileFilter;
import com.gs.utils.swing.table.CollectionTableModel;
import com.gs.utils.text.StringUtil;

/**
 * @author sabuj.das
 *
 */
public class PaginatedTablePanel extends JPanel implements Serializable,
        ActionListener, MouseListener, KeyListener, FocusListener {

    /**
     * Generated serialVersionUID = -4196778703846255569L;
     */
    private static final long serialVersionUID = -4196778703846255569L;
    private static final Logger logger = Logger.getLogger(PaginatedTablePanel.class);
    public static final int MIN_RECORDS_PER_PAGE = 300;
    public static final int MAX_RECORDS_PER_PAGE = 99999;
    private JFrame parentFrame;
    private PaginatedCollection<Log> paginationResult;

    public PaginatedTablePanel(JFrame parentFrame, List<Log> logList) {
        this.parentFrame = parentFrame;

        paginationResult = new PaginatedCollection<Log>(logList, MIN_RECORDS_PER_PAGE);
        paginationResult.setRowsPerPage(MIN_RECORDS_PER_PAGE);
        paginationResult.setStartRow(1);
        initComponents();
        targetTable.addMouseListener(this);
        targetTable.setCellSelectionEnabled(true);
        populatePaginatedResult(1);

    }

    public void populatePaginatedResult(int pageNumber) {
    	paginationResult.setCurrentPage(pageNumber);
    	showTableData();
    }

    private void showTableData() {
    	paginationResult.setPageAttributes(paginationResult.getTotalRows());
    	paginationResult.setEndRow(paginationResult.getStartRow() + paginationResult.getRowsPerPage());
    	List<Log> data = paginationResult.getBatch(paginationResult.getCurrentPage());
    	CollectionTableModel<Log> model = new CollectionTableModel<Log>(data, Log.class.getCanonicalName());
    	targetTable.setModel(model);
        populatePageLinks();
    }

    private void populatePageLinks() {
        if (paginationResult.isPreviousPage()) {
            previousPageLabel.setEnabled(true);
            goToPreviousPageLabel.setVisible(true);
            goToFirstPageLinkLabel.setVisible(true);
        } else {
            previousPageLabel.setEnabled(false);
            goToPreviousPageLabel.setVisible(false);
            goToFirstPageLinkLabel.setVisible(false);
        }
        if (paginationResult.isNextPage()) {
            nextPageLabel.setEnabled(true);
            goToNextPageLinkLabel.setVisible(true);
            goToLastPageLinkLabel.setVisible(true);
        } else {
            nextPageLabel.setEnabled(false);
            goToNextPageLinkLabel.setVisible(false);
            goToLastPageLinkLabel.setVisible(false);
        }

        totaPageslLabel.setText("Page " + paginationResult.getCurrentPage()
                + " of " + paginationResult.getTotalPages() + " page(s). { Total "
                + paginationResult.getTotalRows() + " records. }");

        alterPageNumbers();
    }

    public void alterPageNumbers() {
        resetPageNumbers();
        int first = getFirstPage();
        if (first > 0) {
            firstPageLinkLabel.setText("" + first);
            if (first == paginationResult.getCurrentPage()) {
                firstPageLinkLabel.setForeground(Color.BLUE);
            } else {
                firstPageLinkLabel.setForeground(Color.BLACK);
            }
            if (first + 1 <= paginationResult.getTotalPages()) {
                secondPageLinkLabel.setText("" + (first + 1));
                if (first + 1 == paginationResult.getCurrentPage()) {
                    secondPageLinkLabel.setForeground(Color.BLUE);
                } else {
                    secondPageLinkLabel.setForeground(Color.BLACK);
                }
                if (first + 2 <= paginationResult.getTotalPages()) {
                    thirdPageLinkLabel.setText("" + (first + 2));
                    if (first + 2 == paginationResult.getCurrentPage()) {
                        thirdPageLinkLabel.setForeground(Color.BLUE);
                    } else {
                        thirdPageLinkLabel.setForeground(Color.BLACK);
                    }
                    if (first + 3 <= paginationResult.getTotalPages()) {
                        fourthPageLinkLabel.setText("" + (first + 3));
                        if (first + 3 == paginationResult.getCurrentPage()) {
                            fourthPageLinkLabel.setForeground(Color.BLUE);
                        } else {
                            fourthPageLinkLabel.setForeground(Color.BLACK);
                        }
                        if (first + 4 <= paginationResult.getTotalPages()) {
                            fifthPageLinkLabel.setText("" + (first + 4));
                            if (first + 4 == paginationResult.getCurrentPage()) {
                                fifthPageLinkLabel.setForeground(Color.BLUE);
                            } else {
                                fifthPageLinkLabel.setForeground(Color.BLACK);
                            }
                        }
                    }
                }
            }
        }

    }

    private void resetPageNumbers() {
        firstPageLinkLabel.setText("");
        secondPageLinkLabel.setText("");
        thirdPageLinkLabel.setText("");
        fourthPageLinkLabel.setText("");
        fifthPageLinkLabel.setText("");
    }

    private int getFirstPage() {
        int page = 0;
        if (paginationResult.getTotalPages() <= 5 || paginationResult.getCurrentPage() <= 3) {
            page = 1;
        } else if (paginationResult.getCurrentPage() + 1 > paginationResult.getTotalPages()) {
            page = paginationResult.getCurrentPage() - 4;
        } else if (paginationResult.getCurrentPage() + 2 > paginationResult.getTotalPages()) {
            page = paginationResult.getCurrentPage() - 3;
        } else {
            page = paginationResult.getCurrentPage() - 2;
        }
        return page;
    }

    public void gotoNextPage() {
        if (paginationResult.getCurrentPage() >= paginationResult.getTotalPages()) {
            return;
        }
        paginationResult.setCurrentPage(
                paginationResult.getCurrentPage() + 1);
        gotoPage(paginationResult.getCurrentPage());
    }

    public void gotoPreviousPage() {
        if (paginationResult.getCurrentPage() <= 0) {
            return;
        }
        paginationResult.setCurrentPage(
                paginationResult.getCurrentPage() - 1);
        gotoPage(paginationResult.getCurrentPage());
    }

    public void gotoPage(int pageNumber) {

        populatePaginatedResult(pageNumber);
    }

    /**
     * @return the parentFrame
     */
    public JFrame getParentFrame() {
        return parentFrame;
    }


    /**
     * @param parentFrame the parentFrame to set
     */
    public void setParentFrame(JFrame parentFrame) {
        this.parentFrame = parentFrame;
    }


    private void initComponents() {
        GridBagConstraints gridBagConstraints;

        actionsToolBar = new JToolBar();
        topNavigationPanel = new JPanel();
        previousPageLabel = new JLabel();
        rowsPerPageLabel = new JLabel();
        rowsPerPageTextField = new JTextField();
        refreshButton = new JButton();
        hiddenLabel_01 = new JLabel();
        showActionsToolbarCheckBox = new JCheckBox();
        nextPageLabel = new JLabel();
        tablePanel = new JPanel();
        targetTableScrollPane = new JScrollPane();
        targetTable = new JTable();
        bottomNavigationPanel = new JPanel();
        totaPageslLabel = new JLabel();
        pagerPanel = new JPanel();
        firstPageLinkLabel = new JLabel();
        secondPageLinkLabel = new JLabel();
        thirdPageLinkLabel = new JLabel();
        fourthPageLinkLabel = new JLabel();
        fifthPageLinkLabel = new JLabel();
        goToFirstPageLinkLabel = new JLabel();
        goToPreviousPageLabel = new JLabel();
        goToNextPageLinkLabel = new JLabel();
        goToLastPageLinkLabel = new JLabel();
        gotoPageLabel = new JLabel();
        gotoPageTextField = new JTextField();
        goButtonLabel = new JLabel();

        exportAsLabel = new JLabel();
        exportTypeComboBox = new JComboBox();
        exportButton = new JButton();
        
        ImageIcon image = null;

        setLayout(new GridBagLayout());

        topNavigationPanel.setMinimumSize(new Dimension(100, 30));
        topNavigationPanel.setPreferredSize(new Dimension(710, 30));
        topNavigationPanel.setLayout(new GridBagLayout());

        previousPageLabel.setFont(new Font("Tahoma", 1, 11));
        previousPageLabel.setForeground(new Color(0, 0, 255));
        previousPageLabel.setText("");
        previousPageLabel.setToolTipText("Go to Previous Page");
        image = new ImageIcon(getClass().getResource("/images/"+"previousPage_normal.png"));
        previousPageLabel.setIcon(image);
        previousPageLabel.addMouseListener(this);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.WEST;
        gridBagConstraints.insets = new Insets(0, 2, 0, 0);
        topNavigationPanel.add(previousPageLabel, gridBagConstraints);

        rowsPerPageLabel.setText("Rows per Page:");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 20, 0, 2);
        topNavigationPanel.add(rowsPerPageLabel, gridBagConstraints);

        rowsPerPageTextField.setFont(new Font("Tahoma", 1, 11));
        rowsPerPageTextField.setForeground(new Color(0, 0, 255));
        rowsPerPageTextField.setHorizontalAlignment(JTextField.CENTER);
        rowsPerPageTextField.setText("" + MIN_RECORDS_PER_PAGE);
        rowsPerPageTextField.setMinimumSize(new Dimension(60, 20));
        rowsPerPageTextField.setPreferredSize(new Dimension(60, 20));
        rowsPerPageTextField.addKeyListener(this);
        rowsPerPageTextField.addFocusListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        topNavigationPanel.add(rowsPerPageTextField, gridBagConstraints);

        refreshButton.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+"reload_green.png"));
        refreshButton.setIcon(image);
        refreshButton.addActionListener(this);
        refreshButton.setFocusable(false);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        topNavigationPanel.add(refreshButton, gridBagConstraints);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        topNavigationPanel.add(hiddenLabel_01, gridBagConstraints);

        showActionsToolbarCheckBox.setText("Show Actions");
        showActionsToolbarCheckBox.setSelected(true);
        showActionsToolbarCheckBox.addActionListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 1, 0, 20);
        topNavigationPanel.add(showActionsToolbarCheckBox, gridBagConstraints);

        nextPageLabel.setFont(new Font("Tahoma", 1, 11));
        nextPageLabel.setForeground(new Color(0, 0, 255));
        nextPageLabel.setText("");
        nextPageLabel.setToolTipText("Go to Next Page");
        image = new ImageIcon(getClass().getResource("/images/"+ "nextPage_normal.png"));
        nextPageLabel.setIcon(image);
        nextPageLabel.addMouseListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.insets = new Insets(0, 0, 0, 2);
        topNavigationPanel.add(nextPageLabel, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        add(topNavigationPanel, gridBagConstraints);

        tablePanel.setLayout(new GridBagLayout());

        actionsToolBar.setFloatable(false);
        actionsToolBar.setRollover(true);

        
        exportAsLabel.setText("Export As ... ");
        actionsToolBar.add(exportAsLabel);

        exportTypeComboBox.setModel(
    		new DefaultComboBoxModel(
    			new String[] { 
    				ExportTypeEnum.TEXT.getDescription(),
    				ExportTypeEnum.CSV.getDescription(),
    				ExportTypeEnum.HTML.getDescription(),
    				ExportTypeEnum.EXCEL.getDescription(),
    				ExportTypeEnum.XML.getDescription()
    			}
    		)
    	);
        exportTypeComboBox.setMaximumSize(new Dimension(150, 18));
        exportTypeComboBox.setMinimumSize(new Dimension(150, 18));
        exportTypeComboBox.setPreferredSize(new Dimension(150, 18));
        actionsToolBar.add(exportTypeComboBox);

        exportButton.setText("");
        image = new ImageIcon(getClass()
				.getResource("/images/"
						+ "export_wiz.gif"));
        exportButton.setIcon(image);
        exportButton.addActionListener(this);
        exportButton.setFocusable(false);
        exportButton.setHorizontalTextPosition(SwingConstants.CENTER);
        exportButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        actionsToolBar.add(exportButton);
        actionsToolBar.add(new JToolBar.Separator());

        
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        tablePanel.add(actionsToolBar, gridBagConstraints);

        
        targetTable.setAutoCreateRowSorter(true);
        targetTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        targetTable.setAutoscrolls(true);
        targetTableScrollPane.setViewportView(targetTable);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        tablePanel.add(targetTableScrollPane, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(tablePanel, gridBagConstraints);

        bottomNavigationPanel.setMinimumSize(new Dimension(100, 30));
        bottomNavigationPanel.setPreferredSize(new Dimension(710, 30));
        bottomNavigationPanel.setLayout(new GridBagLayout());

        totaPageslLabel.setText("");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        bottomNavigationPanel.add(totaPageslLabel, gridBagConstraints);

        pagerPanel.setLayout(new GridBagLayout());

        firstPageLinkLabel.setText("");
        firstPageLinkLabel.addMouseListener(this);
        firstPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 4, 0, 4);
        pagerPanel.add(firstPageLinkLabel, gridBagConstraints);

        secondPageLinkLabel.setText("");
        secondPageLinkLabel.addMouseListener(this);
        secondPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 4, 0, 4);
        pagerPanel.add(secondPageLinkLabel, gridBagConstraints);

        thirdPageLinkLabel.setText("");
        thirdPageLinkLabel.addMouseListener(this);
        thirdPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 4, 0, 4);
        pagerPanel.add(thirdPageLinkLabel, gridBagConstraints);

        fourthPageLinkLabel.setText("");
        fourthPageLinkLabel.addMouseListener(this);
        fourthPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 4, 0, 4);
        pagerPanel.add(fourthPageLinkLabel, gridBagConstraints);

        fifthPageLinkLabel.setText("");
        fifthPageLinkLabel.addMouseListener(this);
        fifthPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 4, 0, 4);
        pagerPanel.add(fifthPageLinkLabel, gridBagConstraints);

        goToFirstPageLinkLabel.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+"first_page.png"));
        goToFirstPageLinkLabel.setIcon(image);
        goToFirstPageLinkLabel.addMouseListener(this);
        goToFirstPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 2, 0, 6);
        pagerPanel.add(goToFirstPageLinkLabel, gridBagConstraints);

        goToPreviousPageLabel.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+"previous_page.png"));
        goToPreviousPageLabel.setIcon(image);
        goToPreviousPageLabel.setFont(new Font("Tahoma", 1, 11));
        goToPreviousPageLabel.addMouseListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 2, 0, 6);
        pagerPanel.add(goToPreviousPageLabel, gridBagConstraints);

        goToNextPageLinkLabel.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+"next_page.png"));
        goToNextPageLinkLabel.setIcon(image);
        goToNextPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        goToNextPageLinkLabel.addMouseListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 6, 0, 2);
        pagerPanel.add(goToNextPageLinkLabel, gridBagConstraints);

        goToLastPageLinkLabel.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+"last_page.png"));
        goToLastPageLinkLabel.setIcon(image);
        goToLastPageLinkLabel.setFont(new Font("Tahoma", 1, 11));
        goToLastPageLinkLabel.addMouseListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new Insets(0, 6, 0, 2);
        pagerPanel.add(goToLastPageLinkLabel, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
        bottomNavigationPanel.add(pagerPanel, gridBagConstraints);

        gotoPageLabel.setText("Go To ");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        bottomNavigationPanel.add(gotoPageLabel, gridBagConstraints);

        gotoPageTextField.setHorizontalAlignment(JTextField.RIGHT);
        gotoPageTextField.setMinimumSize(new Dimension(60, 20));
        gotoPageTextField.setPreferredSize(new Dimension(60, 20));
        gotoPageTextField.addKeyListener(this);
        gotoPageTextField.addFocusListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        bottomNavigationPanel.add(gotoPageTextField, gridBagConstraints);

        goButtonLabel.setText("");
        image = new ImageIcon(getClass().getResource("/images/"+ "next.png"));
        goButtonLabel.setIcon(image);
        goButtonLabel.addMouseListener(this);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        bottomNavigationPanel.add(goButtonLabel, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        add(bottomNavigationPanel, gridBagConstraints);
    }

    private JToolBar actionsToolBar;
    private JPanel bottomNavigationPanel;
    private JLabel fifthPageLinkLabel;
    private JLabel firstPageLinkLabel;
    private JLabel fourthPageLinkLabel;
    private JLabel goButtonLabel;
    private JLabel goToFirstPageLinkLabel;
    private JLabel goToLastPageLinkLabel;
    private JLabel goToNextPageLinkLabel;
    private JLabel goToPreviousPageLabel;
    private JLabel gotoPageLabel;
    private JTextField gotoPageTextField;
    private JLabel hiddenLabel_01;
    private JLabel nextPageLabel;
    private JPanel pagerPanel;
    private JLabel previousPageLabel;
    private JButton refreshButton;
    private JLabel rowsPerPageLabel;
    private JTextField rowsPerPageTextField;
    private JLabel secondPageLinkLabel;
    private JCheckBox showActionsToolbarCheckBox;
    private JPanel tablePanel;
    private JTable targetTable;
    private JScrollPane targetTableScrollPane;
    private JLabel thirdPageLinkLabel;
    private JPanel topNavigationPanel;
    private JLabel totaPageslLabel;
    private JLabel exportAsLabel;
    private JButton exportButton;
    private JComboBox exportTypeComboBox;
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(showActionsToolbarCheckBox)) {
        	if(showActionsToolbarCheckBox.isSelected()){
				actionsToolBar.setVisible(true);
			}else{
				actionsToolBar.setVisible(false);
			}
        } else if (e.getSource().equals(refreshButton)) {
            refreshPage();
        } else if(e.getSource().equals(exportButton)){
			ExportTypeEnum dataExportTypeEnum = ExportTypeEnum.getTypeEnumByDescription(
					exportTypeComboBox.getSelectedItem().toString());
			String fileName = getExportOutptFileName(dataExportTypeEnum);
			if(!StringUtil.hasValidContent(fileName)){
				return ;
			}
			
		}
    }

    private String getExportOutptFileName(ExportTypeEnum dataExportTypeEnum) {
        ExtensionFileFilter filter = new ExtensionFileFilter(new String[] { dataExportTypeEnum.getExtension()}, 
				dataExportTypeEnum.getDescription());
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(filter);
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int opt = chooser.showOpenDialog(getParentFrame());
        if (JFileChooser.APPROVE_OPTION == opt) {
            File selectedFile = chooser.getSelectedFile();
            if (selectedFile != null) {
                String fileName = selectedFile.getAbsolutePath();
                
                return fileName;
            }
        }
        return null;
    }

    private void refreshPage(int pageNumber) {
        int recordCount = MIN_RECORDS_PER_PAGE;
        try {
            recordCount = Integer.parseInt(rowsPerPageTextField.getText());
            if (recordCount < MIN_RECORDS_PER_PAGE) {
                recordCount = MIN_RECORDS_PER_PAGE;
            } else if (recordCount > MAX_RECORDS_PER_PAGE) {
                recordCount = MAX_RECORDS_PER_PAGE;
            }
        } catch (NumberFormatException nfe) {
            DisplayUtils.displayMessage(getParentFrame(), "Invalid Number : " + rowsPerPageTextField.getText()
                    + "\nChanging the page size to default [ " + MIN_RECORDS_PER_PAGE + " ].", DisplayTypeEnum.WARN);
            rowsPerPageTextField.setText("" + MIN_RECORDS_PER_PAGE);
        } catch (Exception ex) {
            DisplayUtils.displayMessage(getParentFrame(), ex.getMessage(), DisplayTypeEnum.ERROR);
        }
        paginationResult.setRowsPerPage(recordCount);
        gotoPage(pageNumber);
    }

    private void refreshPage() {
        refreshPage(1);
    }

    /* (non-Javadoc)
     * @see event.MouseListener#mouseClicked(event.MouseEvent)
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        if (MouseEvent.BUTTON1 == e.getButton()) {
            if (e.getClickCount() == 1) {
                if (e.getSource().equals(previousPageLabel) || e.getSource().equals(goToPreviousPageLabel)) {
                    gotoPreviousPage();
                } else if (e.getSource().equals(nextPageLabel) || e.getSource().equals(goToNextPageLinkLabel)) {
                    gotoNextPage();
                } else if (e.getSource().equals(goToFirstPageLinkLabel)) {
                    gotoPage(1);
                } else if (e.getSource().equals(goToLastPageLinkLabel)) {
                    gotoPage(paginationResult.getTotalPages());
                } else if (e.getSource().equals(firstPageLinkLabel)) {
                    int page = Integer.parseInt(firstPageLinkLabel.getText());
                    gotoPage(page);
                } else if (e.getSource().equals(secondPageLinkLabel)) {
                    int page = Integer.parseInt(secondPageLinkLabel.getText());
                    gotoPage(page);
                } else if (e.getSource().equals(thirdPageLinkLabel)) {
                    int page = Integer.parseInt(thirdPageLinkLabel.getText());
                    gotoPage(page);
                } else if (e.getSource().equals(fourthPageLinkLabel)) {
                    int page = Integer.parseInt(fourthPageLinkLabel.getText());
                    gotoPage(page);
                } else if (e.getSource().equals(fifthPageLinkLabel)) {
                    int page = Integer.parseInt(fifthPageLinkLabel.getText());
                    gotoPage(page);
                } else if (e.getSource().equals(goButtonLabel)) {
                    gotoThePage();
                }
            } else if (e.getClickCount() == 2) {
                if (e.getSource().equals(targetTable)) {
                    
                }
            }


        }
    }

    public void gotoThePage() {
        int page = paginationResult.getCurrentPage();
        try {
            page = Integer.parseInt(gotoPageTextField.getText());
            if (page <= 0 || page > paginationResult.getTotalPages()) {
                DisplayUtils.displayMessage(getParentFrame(), "Page not exists : " + gotoPageTextField.getText(), DisplayTypeEnum.ERROR);
            } else {
                gotoPage(page);
            }
        } catch (NumberFormatException nfe) {
            DisplayUtils.displayMessage(getParentFrame(), "Invalid Number : " + gotoPageTextField.getText(), DisplayTypeEnum.ERROR);
            gotoPageTextField.setText("");
        } catch (Exception ex) {
            DisplayUtils.displayMessage(getParentFrame(), ex.getMessage(), DisplayTypeEnum.ERROR);
        }
    }

    public void openQuickEditDialog() {
        
    }

    /* (non-Javadoc)
     * @see event.MouseListener#mouseEntered(event.MouseEvent)
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource().equals(previousPageLabel)) {
            previousPageLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            if (previousPageLabel.isEnabled()) {
                ImageIcon image = new ImageIcon(getClass().getResource("/images/"+"previousPage_over.png"));
                previousPageLabel.setIcon(image);
            }
        } else if (e.getSource().equals(nextPageLabel)) {
            nextPageLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            if (nextPageLabel.isEnabled()) {
                ImageIcon image = new ImageIcon(getClass().getResource("/images/"+"nextPage_over.png"));
                nextPageLabel.setIcon(image);
            }
        } else if (e.getSource().equals(goToFirstPageLinkLabel)) {
            goToFirstPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(goToPreviousPageLabel)) {
            goToPreviousPageLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(firstPageLinkLabel)) {
            firstPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(secondPageLinkLabel)) {
            secondPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(thirdPageLinkLabel)) {
            thirdPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(fourthPageLinkLabel)) {
            fourthPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(fifthPageLinkLabel)) {
            fifthPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(goToNextPageLinkLabel)) {
            goToNextPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(goToLastPageLinkLabel)) {
            goToLastPageLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        } else if (e.getSource().equals(goButtonLabel)) {
            goButtonLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
    }

    /* (non-Javadoc)
     * @see event.MouseListener#mouseExited(event.MouseEvent)
     */
    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource().equals(previousPageLabel)) {
            previousPageLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            ImageIcon image = new ImageIcon(getClass().getResource("/images/"+ "previousPage_normal.png"));
            previousPageLabel.setIcon(image);
        } else if (e.getSource().equals(nextPageLabel)) {
            nextPageLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            ImageIcon image = new ImageIcon(getClass().getResource("/images/"+ "nextPage_normal.png"));
            nextPageLabel.setIcon(image);
        } else if (e.getSource().equals(goToFirstPageLinkLabel)) {
            goToFirstPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(goToPreviousPageLabel)) {
            goToPreviousPageLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(firstPageLinkLabel)) {
            firstPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(secondPageLinkLabel)) {
            secondPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(thirdPageLinkLabel)) {
            thirdPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(fourthPageLinkLabel)) {
            fourthPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(fifthPageLinkLabel)) {
            fifthPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(goToNextPageLinkLabel)) {
            goToNextPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(goToLastPageLinkLabel)) {
            goToLastPageLinkLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e.getSource().equals(goButtonLabel)) {
            goButtonLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }

    /* (non-Javadoc)
     * @see event.MouseListener#mousePressed(event.MouseEvent)
     */
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see event.MouseListener#mouseReleased(event.MouseEvent)
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see event.KeyListener#keyPressed(event.KeyEvent)
     */
    @Override
    public void keyPressed(KeyEvent e) {
    }

    /* (non-Javadoc)
     * @see event.KeyListener#keyReleased(event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent e) {
        if (KeyEvent.VK_ENTER == e.getKeyCode()) {
            if (e.getSource().equals(gotoPageTextField)) {
                gotoThePage();
            } else if (e.getSource().equals(rowsPerPageTextField)) {
                refreshPage();
            }
        }

    }

    /* (non-Javadoc)
     * @see event.KeyListener#keyTyped(event.KeyEvent)
     */
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void focusGained(FocusEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void focusLost(FocusEvent e) {
    }
}
